package com.mycase.im.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycase.im.boot.entity.database.ImFriendRequest;
import com.mycase.im.boot.entity.database.ImUser;
import com.mycase.im.boot.entity.database.ImUserFriend;
import com.mycase.im.boot.exception.CustomException;
import com.mycase.im.boot.mapper.FriendRequestMapper;
import com.mycase.im.boot.mapper.UserFriendMapper;
import com.mycase.im.boot.mapper.UserMapper;
import com.mycase.im.boot.service.FriendRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author ZhuHJay
 * @Date 2021/8/17 15:40
 */
@Service
@Transactional(rollbackFor = CustomException.class)
public class FriendRequestServiceImpl extends ServiceImpl<FriendRequestMapper, ImFriendRequest> implements FriendRequestService {

    @Autowired
    private UserFriendMapper userFriendMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveApply(ImFriendRequest imFriendRequest) {
        if( !save(imFriendRequest) ){
            throw new CustomException("申请发送失败, 请重试");
        }
    }

    @Override
    public List<ImFriendRequest> getMyApply(String reqId) {
        return list(new QueryWrapper<ImFriendRequest>().eq("req_id", reqId).orderByDesc("id"));
    }

    @Override
    public List<ImFriendRequest> getOtherApply(String respId) {
        return list(new QueryWrapper<ImFriendRequest>().eq("resp_id", respId).orderByDesc("id"));
    }

    @Override
    public void replyApply(Integer tId, Integer status) {
        // 同意 ? 数据库修改;互相添加好友 : 数据库修改

        // 查询该表的数据
        ImFriendRequest friendRequest = getOne(new QueryWrapper<ImFriendRequest>().eq("id", tId));
        // 安全判断, 如果状态部位0 那么就不进行后续的操作
        if( friendRequest.getStatus() != 0 ){
            throw new CustomException("该申请已经操作完毕");
        }

        // 数据库修改
        // 创建实体类, 根据非空字段进行修改
        ImFriendRequest imFriendRequest = new ImFriendRequest();
        imFriendRequest.setId(tId);
        imFriendRequest.setStatus(status);
        // 按需修改数据库
        if( !updateById(imFriendRequest) ){
            throw new CustomException("修改出现异常, 请重试");
        }

        // 根据需求判断是否添加好友
        if( status == 1 ){
            // 查询两个人的信息( 用来存放 remark 字段 )
            ImUser reqUser = userMapper.findById(friendRequest.getReqId());
            ImUser respUser = userMapper.findById(friendRequest.getRespId());
            // 相互添加好友信息
            ImUserFriend imUserFriend = new ImUserFriend();
            imUserFriend.setCreateTime(new Date());

            imUserFriend.setUserId(reqUser.getId());
            imUserFriend.setFriendId(respUser.getId());
            imUserFriend.setRemark(respUser.getNikeName());
            // 添加一条数据
            if( userFriendMapper.insert(imUserFriend) != 1 ){
               throw new CustomException("操作失败, 请重试");
            }
            // 再追加一条
            imUserFriend.setUserId(respUser.getId());
            imUserFriend.setFriendId(reqUser.getId());
            imUserFriend.setRemark(reqUser.getNikeName());
            if( userFriendMapper.insert(imUserFriend) != 1 ){
                throw new CustomException("操作失败, 请重试");
            }
        }
    }

    @Override
    public ImFriendRequest getLastApplyById(String fromId, String toId) {
        QueryWrapper<ImFriendRequest> imFriendRequestQueryWrapper = new QueryWrapper<>();
        imFriendRequestQueryWrapper
                .eq("req_id", fromId)
                .eq("resp_id", toId)
                // 等待申请
                .eq("status", 0)
                // 降序排列, 为的就是能够直接拿到最近的一条记录
                .orderByDesc("id");
        return list(imFriendRequestQueryWrapper).get(0);
    }

}
